/**
 * 测试用的精简版框架
 */
//var __global = window;
var __inDeclare = false;  //标识是否处于类声明过程中
var __interfaces = {};
var __pkgName = "";  //当前包的名字
//var __profile = __global.__profile = new Profile();
function LibContext(name){
}
(function(){
	this.__context__ = {};
	this.__classes__ = {
		"alias": function(pattern, dest){
			for(var k in this){
				var name = k.replace(pattern, dest);
				this[name] = this[k];
			}
		}
	};
	this.runtime = null;
	this._package = function(name){
		__pkgName = name;
	};
	this._import = function(className){
		var name = className.split(".").pop();
		if(!(name in this)){
			this[name] = this.__classes__[className];
		}
		/*
		if(className in this.__classes__) return;
		var script = window.document.createElement("script");
		script.type = "text/javascript";
		script.src = "js/classes/" + className.replace(/\./g, "/") + ".js";
		window.document.getElementsByTagName("head")[0].appendChild(script);
		*/
	};
	this._class = function(className, superClass, classImp){
		//runtime.log("define class " + className);
		if(className in this.__classes__){
			window.alert("类重复定义className=" + className);
			return;
		}
		var proto = "prototype";
		/*
		if(superClass === null){
			//如果是null，默认父类为Object（实现中只有AObject继承自Object）
			superClass = Object;
		}else if(superClass === ""){
			//如果是""，默认父类为AObject，保证单根类继承机制
			superClass = __global["AObject"];
		}
		*/
		var _s;
		if(superClass === null){  //默认继承 JS 的原生根对象
			_s = null;  //保证根类 _super 属性为 null
			superClass = Object;  //[TODO]如果框架没有扩展Object，则无需继承Object
		}else{
			if(superClass === ""){
				superClass = this.__classes__["alz.lang.AObject"];
			}
			_s = superClass[proto];
		}

		//定义类
		//var clazz = new Function(...);
		var clazz = function(){
			if(__inDeclare) return;
			//this._clazz.copyProperties(this);
			if(typeof this._init == "function"){
				return this._init.apply(this, arguments);
			}
		};
		//类属性定义
		//var _s = superClass[proto];
		//var _s = superClass ? superClass[proto] : null;
		initClazz.call(clazz, className, _s, classImp);

		//原型定义
		__inDeclare = true;
		var _p = clazz[proto] = new superClass();  //通过原型实现继承
		//[TODO]可以考虑只针对用作子类原型的对象，把对象上的方法(实际在类原型上)拷贝到
		//对象本身上，加速子类对象方法的访问速度，只需到对象本身和父类原型上查询两次。
		__inDeclare = false;
		_p.constructor = clazz;
		if(!_p.__proto__){
			_p.__proto__ = _p;  //fixed for ie
		}
		_p._className = className;  //将类名定义到类的原型上面
		_p._clazz = clazz;
		/*
		_p.__metaclazz__ = function(v){
			for(var k in v){
				this._clazz["_" + k] = v[k];
			}
		};
		_p.__meta__ = function(k, v){
			this._clazz["_" + k] = v;
		};
		*/

		//类绑定
		//保存起来便于管理
		this.__context__[className] = clazz;
		this.__classes__[__pkgName + "." + className] = clazz;
		//this.alz[className] = clazz;  //将类绑定到根包上面
		this[className] = clazz;  //绑定到当前上下文环境（作用域）之上
		//this.runtime._contextList[this.runtime._name][className] = clazz;  //绑定到核心上下文环境之上
		/*
		if(className in __global){
			//window.alert("类已经存在className=" + className);
		}else{
			__global[className] = clazz;  //暴露到全局命名空间下(暴露给global对象，变成全局变量)
		}
		*/

		//执行类的实现代码
		if(typeof classImp == "function"){
			classImp.apply(_p, [_s]);  //function(_super){};  //alzui框架中使用这种方式调用执行
			//clazz.mapProperties(clazz._map);
		}
		//__profile.hackClass(clazz, _p);
		_p = null;
		_s = null;
		clazz = null;
	};
	this._interface = function(name, imp){
		if(!/^I[A-Z]\w+$/.test(name)){
			//runtime.error("[LibContext::_interface]接口名不符合命名规范");
			throw "接口名不符合命名规范";
		}
		__interfaces[name] = imp;
	};
	this._property = function(proto, properties){
	};
	this._implements = function(proto, interfaces){
	};
}).apply(LibContext.prototype);

return {
	regLib: function(name, appName, libImp){
		var className = "LibContext";
		var cxt = new LibContext(name);
		//保证aui命名空间下可以访问到
		cxt.__context__[className] =
		cxt.__classes__["alz." + className] = LibContext;
		//__global[className] = LibContext;
		libImp(cxt);  //第一个参数是新创建的Context实例
		//libImp.apply(cxt);  //this是新创建的Context实例
		var rt = new cxt.__context__["WebRuntime"]();
		LibContext.prototype.runtime = rt;
		__global.__runtime__ = rt;
		rt.init();
		__global.onload = function(){
			rt.onLoad();
			//this.onResize();
		};
		/*
		__global.onresize = function(){
			if(rt){
				rt.onResize();
			}
		};
		*/
		__global.onunload = function(){
			rt.dispose();
			rt = null;
			__global.__runtime__ = null;
			//__global.onresize = null;
			__global.onload = null;
			__global.onunload = null;
		};
	}
};